package cn.tianjindong.blog_manager.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.tianjindong.blog.common.pojo.PageView;
import cn.tianjindong.blog.mapper.ArticleTagMapper;
import cn.tianjindong.blog.mapper.BlogArticleCategoryMapper;
import cn.tianjindong.blog.mapper.BlogArticleCommentMapper;
import cn.tianjindong.blog.mapper.BlogArticleMapper;
import cn.tianjindong.blog.mapper.BlogTagMapper;
import cn.tianjindong.blog.mapper.BlogUserCollectionMapper;
import cn.tianjindong.blog.pojo.ArticleTag;
import cn.tianjindong.blog.pojo.ArticleTagExample;
import cn.tianjindong.blog.pojo.BlogArticle;
import cn.tianjindong.blog.pojo.BlogArticleCategory;
import cn.tianjindong.blog.pojo.BlogArticleCommentExample;
import cn.tianjindong.blog.pojo.BlogArticleExample;
import cn.tianjindong.blog.pojo.BlogManager;
import cn.tianjindong.blog.pojo.BlogTag;
import cn.tianjindong.blog.pojo.BlogTagExample;
import cn.tianjindong.blog.pojo.BlogTagExample.Criteria;
import cn.tianjindong.blog.pojo.BlogUserCollectionExample;
import cn.tianjindong.blog_manager.service.ArticleService;

@Service
public class ArticleServiceImpl implements ArticleService {

	@Autowired
	private BlogArticleMapper articleMapper;

	@Autowired
	private BlogTagMapper tagMaper;

	@Autowired
	private ArticleTagMapper articleTagMapper;
	
	@Autowired
	private BlogArticleCommentMapper commentMapper;
	
	@Autowired
	private BlogUserCollectionMapper collectionMapper;

	@Override
	public void addArticle(BlogManager user, BlogArticle article, String keywords) {
		Date date = new Date();
		article.setCreatetime(date);
		article.setUpdatetime(date);
		article.setManagerId(user.getId());// 设置管理员
		article.setVisitNum(1L);// 设置默认访问数量为1
		articleMapper.insertReturnId(article);
		// 设置文章相关的tag(标签)
		setArticleKeyWords(article, keywords, date);
	}

	@Override
	public void updateArticle(BlogManager user, BlogArticle article, String keywords) {
		Date date = new Date();
		article.setUpdatetime(date);
		article.setManagerId(user.getId());// 设置管理员
		articleMapper.updateByPrimaryKeySelective(article);

		// 删除原先与文章相关的
		ArticleTagExample example = new ArticleTagExample();
		cn.tianjindong.blog.pojo.ArticleTagExample.Criteria criteria = example.createCriteria();
		criteria.andArticleIdEqualTo(article.getId());
		articleTagMapper.deleteByExample(example);

		// 重新设置关键字
		setArticleKeyWords(article, keywords, date);
	}

	/**
	 * 设置文章相关联的tag，tag原先有则只设置关系和文章数加一；如果没有则创建一个再设置关系。
	 * 
	 * @param article
	 * @param keywords
	 * @param date
	 */
	private void setArticleKeyWords(BlogArticle article, String keywords, Date date) {
		// 处理关键字
		if (keywords != null && !keywords.equals("")) {
			String[] split = keywords.split(",|，");
			for (String keyword : split) {
				// 查找关键字名对应的对象
				BlogTagExample example = new BlogTagExample();
				Criteria criteria = example.createCriteria();
				criteria.andTagnameEqualTo(keyword);
				List<BlogTag> list = tagMaper.selectByExample(example);
				BlogTag tag = null;
				if (list == null || list.size() == 0) {// 如果没有，则创建一个
					tag = new BlogTag();
					tag.setArticleNum(1L);
					tag.setCreatetime(date);
					tag.setTagname(keyword);
					tagMaper.insertReturnId(tag);
				} else {
					tag = list.get(0);
					tag.setArticleNum(tag.getArticleNum() + 1);
					// 将文章数目更改添加到数据库
					tagMaper.updateByPrimaryKey(tag);
				}

				// 现在拥有文章对象和关键字对象，需要向表插入多对多的联系数据
				ArticleTag relation = new ArticleTag();
				relation.setArticleId(article.getId());
				relation.setTagId(tag.getId());
				articleTagMapper.insert(relation);
			}
		}
	}

	@Override
	public PageView<BlogArticle> getArticlePageView(int currentPage, int countItem) {

		// 查询时开始索引
		int startIndex = (currentPage - 1) * countItem;
		List<BlogArticle> list = articleMapper.getBriefList(startIndex, countItem);

		// 获取数据库总条数
		int totalItem = articleMapper.countByExample(new BlogArticleExample());

		PageView<BlogArticle> pageView = new PageView<>();
		pageView.setCurrentPage(currentPage);
		pageView.setCountItem(countItem);
		pageView.setItems(list);
		pageView.setTotalItem(totalItem);
		return pageView;
	}

	@Override
	public BlogArticle getArticleById(Long id) {
		if (id != null) {
			return articleMapper.getArticleById(id);
		}
		return null;
	}

	@Override
	public void deleteArticle(Long id) {
		// 先查询这篇文章在数据库是否存在
		BlogArticleExample example = new BlogArticleExample();
		cn.tianjindong.blog.pojo.BlogArticleExample.Criteria criteria = example.createCriteria();
		criteria.andIdEqualTo(id);
		int num = articleMapper.countByExample(example);
		if (num == 1) {
			//删除文章相关联的标签
			ArticleTagExample example2 = new ArticleTagExample();
			example2.createCriteria().andArticleIdEqualTo(id);
			articleTagMapper.deleteByExample(example2);
			//删除文章相关联的评论
			BlogArticleCommentExample example3 = new BlogArticleCommentExample();
			example3.createCriteria().andArticleIdEqualTo(id);
			commentMapper.deleteByExample(example3);
			//删除文章收藏记录
			BlogUserCollectionExample example4 = new BlogUserCollectionExample();
			example4.createCriteria().andArticleIdEqualTo(id);
			collectionMapper.deleteByExample(example4);
			//删除文章
			articleMapper.deleteArticleById(id);
		}
	}

	@Override
	public Long getArticleVisitNum() {
		// TODO 在2.0版本中需在此加入缓存机制。

		Long articeVisitNum = articleMapper.getArticeVisitNum();
		return articeVisitNum;
	}

	@Override
	public Integer getArticleNum() {
		BlogArticleExample example = new BlogArticleExample();
		cn.tianjindong.blog.pojo.BlogArticleExample.Criteria criteria = example.createCriteria();
		criteria.andTabNotEqualTo(-1);// 删除的文章不计在文章个数中
		int countByExample = articleMapper.countByExample(example);
		return countByExample;
	}

}
